package com.king.headline.view.viewPager;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.king.headline.util.UIUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;

/**
 * Created by king on 2016/6/2.
 */
public class PicPagerAdapter extends PagerAdapter {

    private OnMyClickListener mylistener;
    private ArrayList<ItemInfo> _myList;
    private Context context;
    private ImageLoader imageLoader;

    public PicPagerAdapter(Context context, ArrayList<ItemInfo> myList, OnMyClickListener mylistener) {
        imageLoader = UIUtils.getImageLoader();
        _myList = myList;
        this.mylistener = mylistener;
        this.context = context;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    /**
     * 判断是否可以复用条目
     */
    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;

    }

    /**
     * 销毁条目
     */
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    /**
     * 初始化条目
     */
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView = new ImageView(context);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);

        int newposition = position % _myList.size();
        if (_myList != null && _myList.size() > 0) {
            imageLoader.displayImage(_myList.get(newposition).str, imageView, UIUtils.getSimpleOption());
            imageView.setTag(_myList.get(newposition));
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mylistener.myClick(v);
                }
            });
        }

        container.addView(imageView);
        return imageView;
    }

}
